package hn.cch.action;

import com.opensymphony.xwork2.ActionSupport;
import hn.cch.util.FileUtil;
import hn.cch.util.StreamUtil;
import hn.cch.util.StringUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 文件上传
 */

@ParentPackage("struts-default")
@Namespace("/file")
@Component
public class FileAction extends ActionSupport {

    private Logger logger = LoggerFactory.getLogger(FileAction.class);


    private File file;
    private String fileFileName;//文件名
    private String fileContentType;//文件类型



    @Action(value = "upload", results = {
            @Result(name = "index", location = "/index.jsp")
    })
    public String upload(){
        logger.info("file upload");

        String realPath = ServletActionContext.getServletContext().getRealPath("/");

        String filePath = realPath + fileFileName;
        logger.info("filePath : {}", filePath);
        String fileText = StringUtil.fromBytes(FileUtil.toBytes(StreamUtil.toInput(file)));
        logger.info("fileText : {}", fileText);



        return "index";
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
}
